package net.taviscaron.drivenotes.api.action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.taviscaron.drivenotes.api.ResponseUtil;
import net.taviscaron.drivenotes.shared.model.entity.Note;
import net.taviscaron.drivenotes.shared.model.entity.User;
import net.taviscaron.drivenotes.shared.service.NoteService;
import net.taviscaron.drivenotes.web.core.Constants;
import org.springframework.beans.factory.annotation.Required;

/**
 * Notes Action
 * @author Andrei Senchuk
 */
public class NotesAction implements ApiAction {
    private NoteService noteService;

    @Required
    public void setNoteService(NoteService noteService) {
        this.noteService = noteService;
    }

    public Object execute (HttpServletRequest request, HttpServletResponse response) {
        User user = (User) request.getAttribute(Constants.AUTHORIZED_USER_ATTR_NAME);
        List<Note> notes = noteService.findNotesByUser(user);
        return ResponseUtil.successResponse("notes", notes);
    }
}
